package com.zb.jsp.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.jasper.tagplugins.jstl.core.Out;

import com.sun.faces.renderkit.html_basic.OutputMessageRenderer;
import com.zb.jsp.service.UserRegisterService;
import com.zb.jsp.vo.UserRegisterVo;

public class UserRegisterServlet extends HttpServlet {
	
	UserRegisterVo userRegisterVo =new UserRegisterVo();
	UserRegisterService userRegisterservice = new UserRegisterService();
	/**
	 * Constructor of the object.
	 */
	public UserRegisterServlet() {
		super();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//把界面上的信息放到UserRegisterVo
		userRegisterVo.setAge(request.getParameter("age"));
		userRegisterVo.setBloodType(request.getParameter("bloodType").trim());
		userRegisterVo.setDegree(request.getParameter("deGree").trim());
		userRegisterVo.setEmail(request.getParameter("email").trim());
		userRegisterVo.setHeight(request.getParameter("height").trim());
		userRegisterVo.setIncome(request.getParameter("income").trim());
		userRegisterVo.setNameCn(request.getParameter("nameCn").trim());
		userRegisterVo.setPasswd(request.getParameter("passwd").trim());
		userRegisterVo.setProvince( request.getParameter("province").trim());
		userRegisterVo.setSex( request.getParameter("sex").trim());
		userRegisterVo.setWeight( request.getParameter("weight").trim());
		userRegisterVo.setBak1( request.getParameter("bak1").trim());
		userRegisterVo.setBak2( request.getParameter("bak2").trim());
		userRegisterVo.setMarriage( request.getParameter("marriage").trim());
		userRegisterVo.setIndustry( request.getParameter("industry").trim());
		userRegisterVo.setBelief( request.getParameter("belief").trim());
		
		try{
			//调用UserRegisterService方法插入数据到数据库
		userRegisterservice.userRegister(userRegisterVo);
		}catch(Exception e){
			//如出现异常则回到注册页面
			request.getRequestDispatcher("/jsp/rigester.jsp").forward(request, response);
			e.printStackTrace();
		}
		//插入成功的话则返回主页面
		request.getRequestDispatcher("/jsp/main.jsp").forward(request, response);
		PrintWriter out = response.getWriter();
	}

}
